package com.eqdd.yiqidian.ui.select;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.view.View;

import com.eqdd.multitree.tree.bean.Node;
import com.eqdd.multitree.tree.bean.TreeListViewAdapter;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.SelectMultiBumenActivityCustom;
import com.eqdd.yiqidian.adapter.simple.SimpleTreeAdapter;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.DepartmentListResultBean;
import com.eqdd.yiqidian.bean.rebuild.MultiTreeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;


public class SelectMultiBumenActivity extends BaseActivity {
    private SelectMultiBumenActivityCustom dataBinding;
    private ArrayList<DepartmentListResultBean> departmentListResultBeen;
    private ArrayList<MultiTreeBean> datas;
    private SimpleTreeAdapter mAdapter;
    private ArrayList<MultiTreeBean> multiTreeReturn;
    private ArrayList<MultiTreeBean> list;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_select_multi_bumen);
        initTopTitleBar(View.VISIBLE, "选择部门（可多选）");
        initTopRightText("提交", v -> {
            if (!isEmpty()) {
                Intent intent = new Intent();
                intent.putParcelableArrayListExtra("list", multiTreeReturn);
                setResult(Config.SUCCESS, intent);
                finish();
            } else {
                ToastUtil.showLong("请至少选定一个部门");
            }
        });
    }

    private boolean isEmpty() {
        multiTreeReturn = new ArrayList<>();
        Flowable.fromIterable(mAdapter.getOriginData())
                .filter(multiTreeBean -> multiTreeBean.isSelect())
                .subscribe(multiTreeBean -> multiTreeReturn.add(multiTreeBean));
        return multiTreeReturn.size() == 0;
    }

    @Override
    public void initData() {

        datas = new ArrayList<>();
        list = getIntent().getParcelableArrayListExtra("list");


    }

    private void initTreeData(List<DepartmentListResultBean> bean) {
        if (bean == null || bean.size() == 0) {
            return;
        } else {

            for (int i = 0, lenth = bean.size(); i < lenth; i++) {
                int departId = Integer.parseInt(bean.get(i).getDepartId());
                MultiTreeBean multiTreeBean = new MultiTreeBean(departId, Integer.parseInt(bean.get(i).getParentId()), bean.get(i).getDepartName());
                datas.add(multiTreeBean);
                if (list != null) {
                    Flowable.fromIterable(list)
                            .filter(m -> m.getId() == departId)
                            .subscribe(m -> multiTreeBean.setSelect(true));
                }
                initTreeData(bean.get(i).getChilds());
            }
        }
    }

    @Override
    public void setView() {
        OkGo.<HttpResult<List<DepartmentListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.Select_SECTION)
                .params("CompanyId", user.getCompanyId())
                .params("ParentId", 0)
                .execute(new DialogCallBack<HttpResult<List<DepartmentListResultBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<DepartmentListResultBean>>> response) {
                        HttpResult<List<DepartmentListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            departmentListResultBeen = (ArrayList<DepartmentListResultBean>) httpResult.getItems();
                            initTreeData(departmentListResultBeen);
                            try {
                                mAdapter = new SimpleTreeAdapter(dataBinding.listView, SelectMultiBumenActivity.this, datas, 6);
                                mAdapter.setOnTreeNodeClickListener((node, position) -> {
                                    if (node.isLeaf()) {

                                    } else {

                                    }
                                });

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            dataBinding.listView.setAdapter(mAdapter);

                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
